package com.reggie.background.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.background.dto.SetmealDto;
import com.reggie.background.entity.Setmeal;
import com.reggie.background.entity.SetmealDish;
import com.reggie.background.exception.CustomException;
import com.reggie.background.service.SetmealDishService;
import com.reggie.background.service.SetmealService;
import com.reggie.background.mapper.SetmealMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 1
* @description 针对表【setmeal(套餐)】的数据库操作Service实现
* @createDate 2022-07-15 14:14:23
*/
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
    implements SetmealService{

    @Autowired
    private SetmealDishService setmealDishService;
    /**
     * 保存套餐及套餐绑定的菜品信息关系
     * @param setmealDto
     * @return
     */
    @Transactional
    @Override
    public Boolean saveSetmealAndSetmealDish(SetmealDto setmealDto) {
        //先保存套餐信息获取到套餐的id
        boolean result1 = this.save(setmealDto);
        //setmealDishes并插入关联套餐id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<SetmealDish> collect = setmealDishes.stream().map(item -> {
                item.setSetmealId(Long.toString(setmealDto.getId()));
            return item;
        }).collect(Collectors.toList());
        boolean result2 = setmealDishService.saveBatch(collect);
        return result1&&result2;
    }

    /**
     * 更新套餐信息以及更新套餐与菜品关系，（采用删除套餐菜品关系重新插入新的关系数据来代替更新套餐与菜品关系操作）
     * @param setmealDto
     * @return
     */
    @Transactional
    @Override
    public Boolean updateSetmealAndSetmealDish(SetmealDto setmealDto) {
        //先将setmeal数据更新到数据库
        boolean result1 = this.updateById(setmealDto);
        //根据setmealId删除setmealDish菜品和套餐的关系的数据
        LambdaQueryWrapper<SetmealDish> qw=new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, setmealDto.getId());
        boolean result2 = setmealDishService.remove(qw);
        //将前端传来的SetmealDish集合设置套餐关系并插入数据库
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<SetmealDish> collect = setmealDishes.stream().map(item -> {
            item.setSetmealId(Long.toString(setmealDto.getId()));
            return item;
        }).collect(Collectors.toList());
        boolean result3 = setmealDishService.saveBatch(collect);
        return result1&&result3;
    }

    /**
     * 删除停止售卖的套餐
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public Boolean deleteSetmeals(Long[] ids) {
        //先查询套餐信息是否可以删除（正在售卖的不能删除）
        LambdaQueryWrapper<Setmeal> qw=new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getStatus,1);
        qw.in(Setmeal::getId, ids);
        List<Setmeal> list = this.list(qw);
        if (list.size()>0) {
            throw new CustomException("存在有正在售卖的套餐，禁止删除！");
        }
        //删除停止售卖的套餐并删除套餐菜品关系信息
        LambdaQueryWrapper<Setmeal> qw1=new LambdaQueryWrapper<>();
        qw1.in(Setmeal::getId,ids);
        boolean result1 = this.remove(qw1);
        LambdaQueryWrapper<SetmealDish> qw2=new LambdaQueryWrapper<>();
        qw2.in(SetmealDish::getSetmealId,ids);
        boolean result2 = setmealDishService.remove(qw2);
        return result1&&result2;
    }
}




